﻿using ImageManager.Common;
using ImageManager.DBAccess;
using ImageManager.ImageApi;
using ImageManager.ImageApi.AuthenticationInjection;
using ImageManager.ImageService;
using ImageManager.Model;
using ImageManager.MyExcep;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace ImageManager.Controllers
{
    public class DownloadHistoryController : MyBaseController<DownloadHistoryModel>
    {
        protected override BaseServiceProvider<DownloadHistoryModel> getSp()
        {
            return ServiceLoader.GetService<DownloadHistoryServiceProvider>();
        }
        [HttpGet, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public ResponseData<DownloadHistoryModel> ListByImage(string image)
        {
            var result = getSp().List(CondCollection.True().Eq(nameof(DownloadHistoryModel.Image), image));
            return new ResponseData<DownloadHistoryModel>(result);
        }

        [HttpGet]
        public FileContentResult Excel(string period)
        {
            var service = getSp() as DownloadHistoryServiceProvider;
            using (var excel = service.ToExcel(period))
            {
                excel.Seek(0, SeekOrigin.Begin);
                var bytes = new byte[excel.Length];
                excel.Read(bytes, 0, bytes.Length);
                var r = new FileContentResult(bytes, "application/vnd.ms-excel");
                r.FileDownloadName = "下载清单" + period + ".xls";
                return r;
            }
        }
    }
}
